﻿using org.jsoup;
using org.jsoup.nodes;
using System;
using System.Windows.Forms;
namespace CodeBeautifier
{
    public partial class CodeBeautifier : Form
    {
        public CodeBeautifier()
        {
            InitializeComponent();
        }

        public String getCodeType()
        {
            string code_type = null;
            if (radioButton1.Checked == true)
            {
                code_type = "json";
            }
            else if (radioButton2.Checked == true)
            {
                code_type = "xml";
            }
            else if (radioButton3.Checked == true)
            {
                code_type = "html";
            }

            return code_type;
        }

        //格式化
        private void button1_Click(object sender, EventArgs e)
        {

            string code_type = getCodeType();
            if (code_type == null || code_type == "")
            {
                MessageBox.Show("请先选择需要美化的代码语言！");
            }

            string str_source = textBox1.Text;
            if (str_source == null || str_source == "")
            {
                MessageBox.Show("请输入需要美化的代码！");
                return;
            }
            string str_result = null;

            if (code_type == "html")
            {
                Document doc = Jsoup.parse(str_source);
                str_result = doc.outerHtml();
            }
            else if (code_type == "json")
            {
                str_result = CodeFormatAndCompress.FormatForJson(str_source);
            }
            else if (code_type == "xml")
            {
                str_result = CodeFormatAndCompress.FormatForXml(str_source);
            }
            textBox2.Text = str_result;
        }

        //压缩
        private void button2_Click(object sender, EventArgs e)
        {
            string code_type = getCodeType();
            if (code_type == "html")
            {
                string str_source = textBox1.Text;
                Document doc = Jsoup.parse(str_source);
                string str_result = doc.outerHtml();
                textBox2.Text = str_result;
            }
            else if (code_type == "json")
            {
                string str_source = textBox1.Text;
                String str_result = CodeFormatAndCompress.CompressForJson(str_source);
                textBox2.Text = str_result;
            }
            else if (code_type == "xml")
            {
                string str_source = textBox1.Text;
                String str_result = CodeFormatAndCompress.CompressForXml(str_source);
                textBox2.Text = str_result;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
